package com.iknight.theory.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iknight.theory.api.TeConsService;
import com.iknight.theory.mapper.TeConsMapper;
import com.iknight.theory.model.TEConsModel;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TeConsServiceImpl implements TeConsService<TEConsModel, String> {
    private final static Logger logger = LogManager.getLogger(TeConsServiceImpl.class);

    @Autowired
    private TeConsMapper<TEConsModel, String> teConsMapper;

    @Override
    public void save(TEConsModel record) {
        if (null == teConsMapper.selectOne(record.getCons_id())) {
            teConsMapper.insert(record);
            logger.info("执行数据插入成功!");
        } else {
            teConsMapper.update(record);
            logger.info("执行数据更新成功!");
        }

    }

    @Override
    public void delete(String pkey) {
        teConsMapper.delete(pkey);
        logger.info("执行数据删除成功!");
    }

    @Override
    public TEConsModel selectOne(String pkey) {
        TEConsModel result = teConsMapper.selectOne(pkey);
        logger.info("执行数据查询成功!");
        return result;
    }

    @Override
    public PageInfo<TEConsModel> selectPage(Map<String, Object> params, int pageStart, int pageSize) {
        PageHelper.startPage(pageStart, pageSize, true);
        List<TEConsModel> result = teConsMapper.selectByCond(params);
        PageInfo<TEConsModel> pageInfo = new PageInfo<>(result);
        return pageInfo;
    }

    @Override
    public void batchSave(List<TEConsModel> entities) {
        for (TEConsModel entity : entities) {
            this.save(entity);
        }
    }
}
